package com.bojin.studio.ubattery;

import com.google.ads.*;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class InformationFragment extends Fragment {
	
	private TextView tv1=null,tv2=null,tv3=null,tv4=null,tv5=null,tv6=null,tv22=null,tv23=null,tv24=null,tv25=null,tv26=null,tv27=null,tv28=null;
	private String temp1=null,temp2=null,temp3=null,temp4=null,temp5=null,temp6=null;
	private String temp22=null,temp23=null,temp24=null,temp25=null,temp26=null,temp27=null,temp28=null;
	private Button b1=null;
	
    public InformationFragment() {
    	
    }
    
    @Override
	   public void onCreate(Bundle savedInstanceState) {
	       super.onCreate(savedInstanceState);
	       getActivity().registerReceiver(mBatteryInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    }
    private BroadcastReceiver mBatteryInfoReceiver=new BroadcastReceiver()
	{
		
	  @Override
	  public void onReceive(Context context, Intent intent) 
	  {
	   // TODO Auto-generated method stub		 
	   String action=intent.getAction();
	   if(Intent.ACTION_BATTERY_CHANGED.equals(action))
	   {       
	    updateUI(intent);
	   }
	  } 
	 };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	View view=inflater.inflate(R.layout.fragment_information, container, false); 
    	setupViewComponent(view);
    	  /*=============================================================================*/
        //AdView adView = (AdView)getActivity().findViewById(R.id.adView);
       // adView.loadAd(new AdRequest());  
        /*=============================================================================*/
    	return view;
    }
    
    @Override
    public void onDestroy() {
    	getActivity().unregisterReceiver(mBatteryInfoReceiver);
    	super.onDestroy();  
    }
    
    @Override
    public void onResume(){
    	super.onResume();
    	tv1.setText(temp1);
    	tv2.setText(temp2);
    	tv3.setText(temp3);
    	tv4.setText(temp4);
    	tv5.setText(temp5);
    	tv6.setText(temp6);
    	
    	tv22.setText(temp22);
    	tv23.setText(temp23);
    	tv24.setText(temp24);
    	tv25.setText(temp25);
    	tv26.setText(temp26);
    	tv27.setText(temp27);
    	tv28.setText(temp28);
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    	temp1=tv1.getText().toString();
    	temp2=tv2.getText().toString();
    	temp3=tv3.getText().toString();
    	temp4=tv4.getText().toString();
    	temp5=tv5.getText().toString();
    	temp6=tv6.getText().toString();
    	
    	temp22=tv22.getText().toString();
    	temp23=tv23.getText().toString();
    	temp24=tv24.getText().toString();
    	temp25=tv25.getText().toString();
    	temp26=tv26.getText().toString();
    	temp27=tv27.getText().toString();
    	temp28=tv28.getText().toString();
    }
    
    private void setupViewComponent(View view)
    {
    	tv1=(TextView)view.findViewById(R.id.textView9);
    	tv2=(TextView)view.findViewById(R.id.textView10); 
    	tv3=(TextView)view.findViewById(R.id.textView11);
    	tv4=(TextView)view.findViewById(R.id.textView12);
    	tv5=(TextView)view.findViewById(R.id.textView13);
    	tv6=(TextView)view.findViewById(R.id.textView14);
    	
    	tv22=(TextView)view.findViewById(R.id.textView22);
    	tv23=(TextView)view.findViewById(R.id.textView23); 
    	tv24=(TextView)view.findViewById(R.id.textView24);
    	tv25=(TextView)view.findViewById(R.id.textView25);
    	tv26=(TextView)view.findViewById(R.id.textView26);
    	tv27=(TextView)view.findViewById(R.id.textView27);
    	tv28=(TextView)view.findViewById(R.id.textView28);
    	b1=(Button)view.findViewById(R.id.button1);
    	 b1.setOnClickListener(new View.OnClickListener() {  
             public void onClick(View v) {  
            	 Intent intentBatteryUsage = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);        
            	    startActivity(intentBatteryUsage);            
             }  
         });  
    
    }
    
    private void updateUI(Intent intent)
    {
    	BatteryInformation batteryInformation = new BatteryInformation(getActivity(),intent);
    	tv1.setText(batteryInformation.getLevel());
    	tv2.setText(batteryInformation.getStatus());
    	tv3.setText(batteryInformation.getHealth());
    	tv4.setText(batteryInformation.getPlugged());
    	tv5.setText(batteryInformation.getVoltage());
    	tv6.setText(batteryInformation.getTemperature());
    	
    	TimeEstimation te=new TimeEstimation(getActivity(),batteryInformation.getLevelInt());
    	tv22.setText(te.get3gTalk()+" "+getResources().getString(R.string.t_hr));
    	tv23.setText(te.get2gTalk()+" "+getResources().getString(R.string.t_hr));
    	tv24.setText(te.getInternetMobile()+" "+getResources().getString(R.string.t_hr));
    	tv25.setText(te.getInternetWifi()+" "+getResources().getString(R.string.t_hr));
    	tv26.setText(te.getVideoPlay()+" "+getResources().getString(R.string.t_hr));
    	tv27.setText(te.getAudioPlay()+" "+getResources().getString(R.string.t_hr));
    	tv28.setText(te.getStandby()+" "+getResources().getString(R.string.t_hr));
    }
}
